package com.springmvc.utils.method;

import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.springmvc.cache.CacheKey;
import com.springmvc.cache.MapCacheManager;
import com.springmvc.entity.Majiang;
import com.springmvc.entity.MethodEnum;
import com.springmvc.utils.JsonUtils;
import com.springmvc.utils.majiang.ChannelUtil;
import com.springmvc.utils.result.ResultMsg;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class MajiangUtil {

	static MapCacheManager cache = MapCacheManager.getInstance();

	/**
	 * 麻将开始分派
	 * @Title: startUserMajiang 
	 * @param roomid
	 * @return void    返回类型 
	 * @throws
	 */
	public static void startUserMajiang(Long roomid) {
		UserUtil.setMajiang(roomid, 1, getMj(roomid, 13));
		UserUtil.setMajiang(roomid, 2, getMj(roomid, 13));
		UserUtil.setMajiang(roomid, 3, getMj(roomid, 13));
		UserUtil.setMajiang(roomid, 4, getMj(roomid, 13));

	}

	private static List<String> getMj(Long roomid, int num) {
		Deque<String> majiang = (Deque<String>) cache.get(CacheKey.setMajiangKey(roomid));
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < num; i++) {
			list.add(majiang.removeFirst());
		}
		return list;
	}

	//============================================

	public static void getMajiang(Long roomid) {
		Deque<String> majiang = (Deque<String>) cache.get(CacheKey.setMajiangKey(roomid));
		List<Channel> list = ChannelUtil.getChannelList(roomid);
		for (Channel channel : list) {
			channel.writeAndFlush(new TextWebSocketFrame(majiang.toString()));
		}
	}

	public static void queueMajiang(Long roomid, Long userid, Integer position) {
		MapCacheManager cache = MapCacheManager.getInstance();
		Deque<String> majiang = (Deque<String>) cache.get(CacheKey.setMajiangKey(roomid));
		Channel channel = ChannelUtil.getChannel(userid);
		channel.writeAndFlush(new TextWebSocketFrame(majiang.removeFirst()));
	}

	public static void setMajiang(Long roomid) {
		cache.set(CacheKey.setMajiangKey(roomid), Majiang.getMJDeque());
	}

	public static void playmj(Long roomid, Integer position, String mj) {
		MapCacheManager cache = MapCacheManager.getInstance();
		Long userid = (Long) cache.get(CacheKey.setRoomPositionUseridKey(roomid, position));
		if (userid == null) {
			System.err.println("用户不存在");
		}
		Channel channel = (Channel) cache.get(CacheKey.setChannelKey(userid));
		if (channel == null) {
			System.err.println("channel为空");
		}

		List<String> list = (List<String>) cache.get(CacheKey.setUsermajiangKey(roomid, position));
		for (int i = 0; i < list.size(); i++) {
			if (StringUtils.equalsIgnoreCase(list.get(i), mj)) {
				list.remove(i);
				break;
			}
		}

		List<Channel> channels = ChannelUtil.getChannelList(roomid);
		for (Channel channel2 : channels) {
			if (channel2 != channel) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("position", position);
				map.put("mj", mj);
//				String json = JsonUtils.obj2JsonString(ResultMsg.SUCCESS(MethodEnum.play, map));
//				channel2.writeAndFlush(new TextWebSocketFrame(json));
			}
		}

	}

}
